<?php
/*******************************************************
*
* Page:    init.php
* Author:  Matthew Sturges (matt@matthewsturges.com)
* Purpose: Initialization file for ws_app toolset
* Created: 06/29/05
* Version: 2.0
*
* Modified:
*    01/01/05 AAA  Sample modification notes
*
*******************************************************/

//Require the necessary files
require_once 'ws_app/config.php';
require_once 'ws_app/app_lib.class';
require_once 'ws_app/app_data.class';
require_once 'ws_app/app_list.class';
require_once 'ws_app/app_form.class';
require_once 'ws_app/app_object.class';
require_once 'ws_app/app_cache.class';
require_once 'ws_app/app_debug.class';

//Create the $DEBUG object, passing the debug flag so it knows whether or not to actually do anything.
$DEBUG = new app_debug(APP_DEBUG);


//Create the database connection
app_data::connect();

//Load the cache file
$APP_CACHE = new app_cache();

//Look for init files in the application directories
$doc_root = $_SERVER['DOCUMENT_ROOT'];
$dir_handle = opendir($doc_root);
$prefix = APP_CUSTOM_DIR_INCLUDE_PREFIX;
$prefix_length = strlen($prefix);
$load_init = false; //set the flag.
while (false !== ($file = readdir($dir_handle))) {
    //Does the folder have a 'ws_' prefix?
    if (substr($file, 0, 3) == 'ws_'){
        $load_init = true;
    } elseif ($prefix_length > 0){
        //If not, does its prefix match the custum dir include prefix?
        if (substr($file, 0, $prefix_length) == $prefix){
            $load_init = true;
        }//end if

    }//end elseif

    //If there was a match above, load the init file for the dir, if one exists
    if ($load_init){
        if (is_dir($doc_root . '/' . $file)){
            if (file_exists($doc_root . '/' . $file . '/init.php')){
                require_once $doc_root . '/' . $file . '/init.php';
                $DEBUG->STASH('note', "Added $file init.php to application");
            }//end if

        }//end if

    }//end if
    //reset the flag
    $load_init = false;

}//end while
?>
